package com.zhiyizhen.handheld.component.pharmacy.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.zhiyizhen.handheld.R;
import com.zhiyizhen.handheld.common.view.adapter.recyclerview.BaseAdapter;
import com.zhiyizhen.handheld.common.view.adapter.recyclerview.BaseViewHolder;
import com.zhiyizhen.handheld.component.pharmacy.bean.RegimenRecordBean;
import com.zhiyizhen.handheld.databinding.ItemRegimenHistoryBinding;

/**
 *
 */
public class RegimenHistoryAdapter extends BaseAdapter<RegimenRecordBean, RegimenHistoryAdapter.RegimenHistoryViewHolder> {
    private Context context;

    public RegimenHistoryAdapter(Context context) {
        super(context);
        this.context = context;
    }

    @Override
    public int getItemViewType(int position) {
        return super.getItemViewType(position);
    }

    @NonNull
    @Override
    public RegimenHistoryViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new RegimenHistoryViewHolder(this, ItemRegimenHistoryBinding.inflate(LayoutInflater.from(parent.getContext())));
    }

    class RegimenHistoryViewHolder extends BaseViewHolder<RegimenRecordBean> {

        private final ItemRegimenHistoryBinding binding;

        public RegimenHistoryViewHolder(BaseAdapter<RegimenRecordBean, RegimenHistoryViewHolder> adapter, ItemRegimenHistoryBinding viewBinding) {
            super(adapter, viewBinding.getRoot());
            binding = viewBinding;
        }

        @Override
        public void bindData(RegimenRecordBean data, int position) {
            binding.dateTv.setText(data.getDispenseTime());
            LinearLayoutManager layoutManager = new LinearLayoutManager(context);
            layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
            binding.recyclerView.setLayoutManager(layoutManager);
            binding.recyclerView.setAdapter(new RegimenRecordDetailAdapter(context, data.getRxList()));

            binding.infoLayout.setVisibility(View.GONE);
            binding.moreImg.setImageResource(R.drawable.icon_up_arrow);

            binding.moreImg.setOnClickListener(v -> {
                if (binding.infoLayout.getVisibility() == View.VISIBLE) {
                    binding.infoLayout.setVisibility(View.GONE);
                    binding.moreImg.setImageResource(R.drawable.icon_up_arrow);
                } else {
                    binding.infoLayout.setVisibility(View.VISIBLE);
                    binding.moreImg.setImageResource(R.drawable.icon_down_arrow);
                }
            });
            binding.dateTv.setOnClickListener(v -> {
                if (binding.infoLayout.getVisibility() == View.VISIBLE) {
                    binding.infoLayout.setVisibility(View.GONE);
                    binding.moreImg.setImageResource(R.drawable.icon_up_arrow);
                } else {
                    binding.infoLayout.setVisibility(View.VISIBLE);
                    binding.moreImg.setImageResource(R.drawable.icon_down_arrow);
                }
            });

            if (TextUtils.equals(data.getDeliveryStatus(), "3")) {
                binding.rejectPersonLayout.setVisibility(View.VISIBLE);
                binding.rejectTimeLayout.setVisibility(View.VISIBLE);
                binding.rejectReasonLayout.setVisibility(View.VISIBLE);
                binding.rejectPersonTv.setText(data.getDeliveryUserName());
                binding.rejectTimeTv.setText(data.getDeliveryTime());
                binding.rejectReasonTv.setText(data.getRefuseReason());
            } else {
                binding.rejectPersonLayout.setVisibility(View.GONE);
                binding.rejectTimeLayout.setVisibility(View.GONE);
                binding.rejectReasonLayout.setVisibility(View.GONE);
            }

            binding.personTv.setText(data.getDispenseUserName());
            binding.timeTv.setText(data.getDispenseTime());
            binding.contentLayout.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
        }
    }
}
